<template>
  <div>
    <el-row type="flex" class="row-bg btn-row" justify="end">
      <el-button size="mini" @click="add" type="primary" icon="el-icon-plus">添加用户</el-button>
    </el-row>
    <el-table :data="listData.items" v-loading.body="listLoading" @sort-change="sortlist" size="mini" stripe border fit highlight-current-row>
        <el-table-column prop="id" label="用户ID" width="100"></el-table-column>
      <el-table-column prop="name" label="用户名称"></el-table-column>
      <el-table-column prop="enable" label="状态" width="100">
        <template slot-scope="scope"><el-tag size="mini" :type="scope.row.enable?null:'info'">{{scope.row.enable?'激活':'禁用'}}</el-tag></template>
      </el-table-column>
      <el-table-column prop="created_at" label="创建时间"></el-table-column>
      <el-table-column prop="updated_at" label="更新时间"></el-table-column>
      <el-table-column label="操作" width="150" >
        <template slot-scope="scope">
          <div>
            <el-button type="primary" size="mini" @click="update(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="remove(scope.row)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination  @size-change="handleSizeChange" @current-change="handleCurrentChange"
                      :current-page="q.page_no" :page-sizes="$store.state.app.pageSizeArr"
                      :page-size="q.page_size"
                      layout="total, sizes, prev, pager, next, jumper"
                      :total="listData.total">
      </el-pagination>
    </div>
    <save-update  v-if="showUpdate" :showUpdate.sync="showUpdate" :id="show_id" :rolelist="rolelist"></save-update>
  </div>
</template>

<script>
  import saveUpdate from './saveUpdate'
  import { getUserList, deleteUser, getRoleList } from '@/api'
  export default {
    components: {saveUpdate},
    data() {
      return {
        listData: {},
        q: {page_size: 30, page_no: 1},
        listLoading: false,
        showUpdate: false,
        show_id: null,
        rolelist: {},
      }
    },
    created() {
      this.getRoleList()
    },
    mounted(){
      this.getList()
    },
    deactivated(){
      this.listLoading = false
    },
    methods: {
      add(){
        this.show_id = null
        this.showUpdate = true
      },
      update(row){
        this.show_id = row.id
        this.showUpdate = true
      },
      remove(row){
        this.$confirm(`确认删除ID为${row.id}的项么？`, '删除用户', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteUser(row.id).then(res => {
            if(res.status === 0){
              this.$message({ type: 'success', message: '删除成功!' })
              this.getList()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      query(){
        this.q = Object.assign(this.q, {page_no: 1, page_size: 30})
        this.getList()
      },
      reload() {
        this.q = {page_size: 30, page_no: 1}
        this.getList()
      },
      sortlist(sort){
        this.q.$orderby = sort.prop
        this.getList()
      },
      handleSizeChange (val) {
        this.q.page_size = val
        this.getList()
      },
      handleCurrentChange (val) {
        this.q.page_no = val
        this.getList()
      },
      getRoleList(){
        getRoleList().then(res => {
          if (res.status === 0) {
            let Array = res.data.items
            Array.map((item)=>{
              this.rolelist[item.id] = item.name
            })
          }
        })
      },
      getList(){
        getUserList(this.q).then(res => {
          if(res.status === 0){
            this.listLoading = false
            this.listData = res.data
          }
        })
      }
    },
    watch: {
      showUpdate() {
        if(!this.showUpdate){
          this.getList()
        }
      }
    }
  }
</script>
